package tree.tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    // 前序遍历
    void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    // 获取树中节点的个数
    int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }

    //  获取叶子节点的个数
    int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left)
                + getLeafNodeCount(root.right);
    }

    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1)
                + getKLevelNodeCount(root.right, k - 1);
    }

    // 获取二叉树的高度
    int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        return left > right ? left + 1 : right + 1;
    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        //到这里确定不是空
        if (root.val == val) {
            return root;
        }
        //判断val是否相同
        TreeNode left = find(root.left, val);
        if (left != null) {
            return left;
        }
        TreeNode right = find(root.right, val);
        if (right != null) {
            return right;
        }
        return null;
    }

    /**
     * 1. 检查两颗树是否相同。
     * https://leetcode.cn/problems/same-tree/
     *
     * @param p
     * @param q
     * @return
     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //判断是否单方为空 如果是 就返回false
        if ((p == null && q != null) || (p != null && q == null)) {
            return false;
        }
        //同时为null 就代表当前是一样的
        if (p == null && q == null) {
            return true;
        }
        //现在双方都不为空 判断val 值 如果不一样返回false
        if (p.val != q.val) {
            return false;
        }
        //开始递归 只要有一边返回是 false 值就是false
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }

    /**
     * 另一颗树的子树。
     * https://leetcode.cn/problems/subtree-of-another-tree/
     *
     * @param root
     * @param subRoot
     * @return
     */
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null || subRoot == null) {
            return false;
        }
        if (isSameTree(root, subRoot)) {
            return true;
        }
        if (isSubtree(root.left, subRoot)) {
            return true;
        }
        if (isSubtree(root.right, subRoot)) {
            return true;
        }
        return false;
    }

    /**
     * 3. 翻转二叉树。
     * https://leetcode-cn.com/problems/invert-binary-tree/
     */
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        if (root.left == null && root.right == null) {
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    /**
     * 4. 判断一颗二叉树是否是平衡二叉树。
     * https://leetcode.cn/problems/balanced-binary-tree/
     *
     * @param root
     * @return
     */
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        return Math.abs(getHeight2(root.left) - getHeight2(root.right)) <= 1
                && isBalanced(root.left) && isBalanced(root.right);
    }

    int getHeight2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = getHeight2(root.left);
        int right = getHeight2(root.right);
        return left > right ? left + 1 : right + 1;
    }

    //层序遍历
    void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }
        }
    }
//    public List<List<Integer>> levelOrder(TreeNode root) {
//        List<List<Integer>> list = new ArrayList<>();
//        Queue<TreeNode> queue = new LinkedList<>();
//        queue.add(root);
//        while (!queue.isEmpty()) {
//            List<Integer> ret = new ArrayList<>();
//            int size = queue.size();
//            while(size != 0) {
//                TreeNode cur = queue.poll();
//                ret.add(cur.val);
//                if (cur.left != null) {
//                    queue.add(cur.left);
//                }
//                if (cur.right != null) {
//                    queue.add(cur.right);
//                }
//                size--;
//            }
//            list.add(ret);
//        }
//        return list;
//    }
     //判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root){
        if (root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                queue.add(cur.left);
                queue.add(cur.right);
            }else {
                break;
            }

        }
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null ) {
                return false;
            }
        }
        return true;
    }
    /**
     * 二叉树的锯齿形层序遍历
     *没完成
     * https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/description/
     */
//    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
//        List<List<Integer>> list = new ArrayList<>();
//        Stack<TreeNode> s1 = new Stack<>();
//        Stack<TreeNode> s2 = new Stack<>();
//        //放root 进入 s2 栈
//        s2.push(root);
//        while(!s1.empty() && !s2.empty()) {
//            //弹出所有s1 栈
//            List<Integer> cur1 = new ArrayList<>();
//            while(!s2.empty()) {
//                TreeNode cur = s1.pop();
//                //将s2 下层的放入s1 栈
//                if(cur != null) {
//                    s1.push(cur.left);
//                    s1.push(cur.right);
//                    cur1.add(cur.val);
//                }
//            }
//            list.add(cur1);
//            List<Integer> cur2 = new ArrayList<>();
//            while(!s1.empty()) {
//                TreeNode cur = s1.pop();
//                //将s1 下层的放入s2 栈
//                if(cur != null) {
//                    s1.push(cur.right);
//                    s1.push(cur.left);
//                    cur2.add(cur.val);
//                }
//            }
//            list.add(cur2);
//        }
//        return list;
//    }

    /**
     * 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先 。
     * https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/
     * @param root
     * @param p
     * @param q
     * @return
     */
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        if(root == p || root == q) {
            return root;
        }
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);
        if(left != null && right != null) {
            return root;
        }else if(left != null) {
            return left;
        }else {
            return right;
        }
    }
    //方法二 用栈 先找到线路 ，然后在先走多的那几步，然后再一起走
    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();
        getPath(root,p,stackP);
        getPath(root,q,stackQ);
        int sizeP = stackP.size();
        int sizeQ = stackQ.size();
        if(sizeP > sizeQ) {
            int size = sizeP - sizeQ;
            while(size != 0) {
                stackP.pop();
                size--;
            }
        }else {
            int size = sizeQ - sizeP;
            while(size != 0) {
                stackQ.pop();
                size--;
            }
        }
        while(!stackP.empty() && !stackQ.empty() ) {
            if(stackP.peek() == stackQ.peek()) {
                return stackP.peek();
            }else {
                stackP.pop();
                stackQ.pop();
            }
        }
        return null;
    }
    private boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack) {
        if (root == null || node == null) {
            return false;
        }
        stack.push(root);
        if (root == node) {
            return true;
        }
        boolean left = getPath(root.left,node,stack);
        if(left) {
            return true;
        }
        boolean right = getPath(root.right,node,stack);
        if(right) {
            return true;
        }
        stack.pop();
        return false;
    }
}

